//+------------------------------------------------------------------+
//|                                                   commission.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#import "Wininet.dll"
   int InternetOpenA(string, int, string, string, int);
   int InternetConnectA(int, string, int, string, string, int, int, int); 
   int HttpOpenRequestA(int, string, string, int, string, int, string, int); 
   int InternetOpenUrlA(int, string, string, int, int, int);
   int InternetReadFile(int, string, int, int& OneInt[]);
   int InternetCloseHandle(int); 
   int InternetCloseHandle(int); 
   int InternetCloseHandle(int); 
#import

extern string brokerName = "exness";
extern string CommentStartWith = "agent '";
extern string CommentEndWith = "'";
extern string dataUrl = "http://127.0.0.1:8080/webapp/data/ea";
extern double svrPort = 8080;

datetime LastCheckTime = 0;
bool needStop = false;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {


   if(needStop){
      if(TimeLocal() - LastCheckTime > 3600){
         needStop = false;
      }
      return (0);
   }

  if(TimeLocal() - LastCheckTime < 10){
      return (-1);
   }
//----
  int i,hstTotal=OrdersHistoryTotal();
  for(i=0;i<hstTotal;i++){
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false){
        Print("Error: ", GetLastError());
        break;
     }
     
     if(OrderType() == 6){
       if(OrderOpenTime() < LastCheckTime){
          continue;
       }
     
       int start = StringFind(OrderComment(), CommentStartWith, 0);
       if(start > -1){
         start = start + StringLen(CommentStartWith);
         int end = StringFind(OrderComment(), CommentEndWith, start);
         string accountNo = StringSubstr(OrderComment(), start, end - start);
         
         uploadData(accountNo, OrderProfit(), OrderOpenTime());
         
         
         
       }
     }
   }
   
//----
   LastCheckTime = TimeLocal();
   return(0);
  }
//+------------------------------------------------------------------+


void uploadData(string accountNo, double commission, datetime inTime){
   Print(accountNo, ": ", commission, "@", inTime);
   
   
   
   int iSession = 0;
   int iConnect = 0;
   int iRequest = 0;
   string sHeader = "Yockii Commission Data";
   string postData = "brokerName=" + brokerName + "&accountNo=" + accountNo + "&commission=" + commission + "&inTime="+inTime;
   
   iSession = InternetOpenA(sHeader, 0, "", "", 0);
   iConnect = InternetConnectA(iSession, "", svrPort, "", "", 3, 0, 1);
   iRequest = InternetOpenUrlA(iSession, dataUrl + "?" + postData, NULL, 0, 0, 0);
   
   
   int read[1];
   string buffer = " ";
   string page = "";
   
   while(true){
     InternetReadFile(iRequest, buffer, StringLen(buffer), read);
     if(read[0] > 0) page = page + StringSubstr(buffer, 0, read[0]);
     else break;
   }
   
   Print(page);
   
   if(iRequest > 0) InternetCloseHandle(iRequest);
   if(iConnect > 0) InternetCloseHandle(iConnect);
   if(iSession > 0) InternetCloseHandle(iSession);
   
   if(page == "-99"){
      Comment("Server did not response a correct data, due to request header is Wrong!!!!!!");
      needStop = true;
   }
   //Print(postData);
}